<?php
// Functions for the Linux Counter webpages


function requirelogin() {
// Check that the login cookie exists
   //global $HTTP_HOST;		// envar
   //global $REQUEST_URI;		// envar
   //global $login;		// cookie
   //global $nocookie;            // auth as a non-cookie
   global $counter;

   $HTTP_HOST = getenv("HTTP_HOST");
   $REQUEST_URI = getenv("REQUEST_URI");
   $login = $counter->get_input("login", "", "cookie");
   $nocookie = $counter->get_input("nocookie", "", "get");

   if (!$login && !$nocookie) {
	header("Status: 302 please login first");
 	$selfurl = "http://$HTTP_HOST/$REQUEST_URI";
	header("Location: http://$HTTP_HOST/person/login.php?url=$selfurl");
        exit;
   }
}

function currentuser() {
// Return the user number from the cookie.
   global $counter;

   $login = $counter->get_input("login", "", "cookie");
   $nocookie = $counter->get_input("nocookie", "", "get");
   if (!$login && $nocookie) {
	$login = $nocookie;
   }
   if (ereg("([0-9]+)[/:]", $login, $res)) {
	return $res[1];
   } else {
	return "";
   }
}

function maybecookie($sep = "?") {
// Print the nocookie cookie if given, otherwise nothing
   global $nocookie;

   if ($nocookie) {
      print $sep;
      print "nocookie=" . urlencode($nocookie);
   }
}

function radio($field, $value, $checkifeq) {
// Radio tag
   $result = 0;
   echo "<input NAME=\"$field\" type=radio value=\"$value\" ";
   if ($checkifeq == $value) {
	echo "checked";
	$result = 1;
   }
   echo ">";
   return $result;
}

?>
